//
//  WFCheckBoxView.m
//  WisdomFarm
//
//  Created by sky on 2018/2/18.
//  Copyright © 2018年 sky. All rights reserved.
//

#import "WFCheckBox.h"

@interface WFCheckBox()

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation WFCheckBox

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 4;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor backgroundColor].CGColor;

        _textLabel = [[UILabel alloc] init];
        self.textLabel.textColor = [UIColor textTitleColor];
        self.textLabel.font = SCFont(14);
        [self addSubview:_textLabel];
        _checkIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"report_box_check"]];
        _checkIcon.hidden = YES;
        [self addSubview:_checkIcon];
        [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
        }];
        [_checkIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.offset(0);
        }];
        
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:self.tap];
        
        self.checked = NO;
    }
    return self;
}

- (void)dealloc {
    [self removeGestureRecognizer:self.tap];
}

- (void)setChecked:(BOOL)checked {
    if (_checked == checked) {
        return;
    }
    _checked = checked;
    self.textLabel.textColor = checked ? [UIColor themeColor] : [UIColor textTitleColor];
    self.checkIcon.hidden = !checked;
    self.layer.borderColor = checked ? [UIColor themeColor].CGColor : [UIColor backgroundColor].CGColor;
}

- (void)tap:(UITapGestureRecognizer *)tap {
    if (self.onCheck) {
        self.onCheck(self);
    } else {
        self.checked = !self.checked;
    }
}

@end
